package cc.sheji.controller.admin;

import cc.sheji.Service.AdminService;
import cc.sheji.domain.Ad;
import cc.sheji.domain.Admin;
import cc.sheji.domain.JsonResult;
import cc.sheji.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Validated
@RestController
@RequestMapping(value = "/admin/v1/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @PostMapping("/logout")
    public JsonResult logout() {

        return JsonResult.Success();
    }


    @RequestMapping("login")
    public JsonResult login(@RequestBody @Validated(User.AddeGroup.class) Admin entity) throws Exception {
        return JsonResult.Success(adminService.adminLogin(entity.getName(), entity.getPassword()));
    }

    @RequestMapping("/info")
    public JsonResult get() {
        return JsonResult.Success(adminService.getAdmin());
    }

    @PostMapping("register")
    public JsonResult create(@RequestBody @Validated(User.AddeGroup.class) Admin user) {
        user.setId(null);
        user.setAvator("https://img0.baidu.com/it/u=4060770951,4069855872&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
            return JsonResult.Success(adminService.create(user));

    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @Valid Admin user) {

        user.setId(id);

        return JsonResult.Success(adminService.update(user));
    }

}
